import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';


@Injectable()
export class GetDataService {

  constructor(
  	private http: Http
  ) { }

  getShuiLunJiParam(): Observable<any>{
  	return new Observable<any>(observer => {
  		this.http.get("waterplant/staticData/unit/turbineParam/4")
  			.map(this.extractData)
  			.subscribe(
  				res => {
  					observer.next(res);
  					observer.complete();
  				}
  			)
  	})
  }
  getFdjParam():Observable<any>{
    return new Observable<any>(observer => {
      this.http.get("waterplant/staticData/unit/generatorParam/4")
        .map(this.extractData)
        .subscribe(
          res => {
            observer.next(res);
            observer.complete();
          }
        )
    })
  }
  getJzParam():Observable<any>{
    return new Observable<any>(observer => {
      this.http.get("waterplant/staticData/unit/unitParam/4")
        .map(this.extractData)
        .subscribe(
          res => {
            observer.next(res);
            observer.complete();
          }
        )
    })
  }
  private extractData(res: Response) {
    let body = res.json();
    return body || {};
  }

  private handleError(error:Response|any) {
    let errmsg :string;
    if(error instanceof Response){
      const body = error.json()||{};
      const err = body.error || JSON.stringify(body);
      errmsg=`${error.status}--${error.statusText||''}${err}`;
    }
    else{
      errmsg=error.message?error.message:error.toString();
    }
    console.log(errmsg);
    return Observable.throw(errmsg);
  }
}
